/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 * File: gain_ctrl.c
 *
 * MATLAB Coder version            : 5.1
 * C/C++ source code generated on  : 09-Jul-2024 17:31:51
 */

/* Include Files */
#include "gain_ctrl.h"
#include <math.h>

/* Function Definitions */
/*
 * Arguments    : float pre
 *                float act
 *                float factor
 * Return Type  : float
 */
float gain_ctrl(float pre, float act, float factor)
{
  float out;
  out = fabsf(pre - act) / factor;
  if (((pre > 0.0F) && (act >= 0.0F)) || ((pre > 0.0F) && (act <= 0.0F)) ||
      ((pre == 0.0F) && (act < 0.0F))) {
    out += act;
    if (out > pre) {
      out = pre;
    }
  } else if (((pre < 0.0F) && (act <= 0.0F)) || ((pre < 0.0F) && (act >= 0.0F)) ||
             ((pre == 0.0F) && (act > 0.0F))) {
    out = act - out;
    if (out < pre) {
      out = pre;
    }
  } else {
    out = 0.0F;
  }

  return out;
}

/*
 * File trailer for gain_ctrl.c
 *
 * [EOF]
 */
